<template>
  <div>
    <h3>注册页面</h3>
    <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="用户名" prop="username">
            <el-input v-model="ruleForm.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
            <el-input type="password" v-model="ruleForm.password" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="password2">
            <el-input type="password" v-model="ruleForm.password2" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="手机号" prop="mobile">
            <el-input v-model.number="ruleForm.mobile"></el-input>
        </el-form-item>

        <el-form-item label="验证码" prop="imagecode">
            <el-input v-model="ruleForm.imagecode"></el-input>
            <img :src="imageUrl" alt="图片验证码" @click="getImage">
        </el-form-item>
        <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">注册</el-button>
        <!-- <el-button @click="resetForm('ruleForm')">重置</el-button> -->
  </el-form-item>
</el-form>
  </div>
</template>

<script>
import {v4 as uuid} from 'uuid'
import axios from 'axios'
  export default {
    data() {
      var validaUser = (rule, value, callback) => {
        if (!value) {
          return callback(new Error('用户名不能为空'));
        }
        const userRules=/^[a-zA-Z][a-zA-Z0-9]{4,15}$/
        if(userRules.test(value)){
            axios.get('http://127.0.0.1:8000/user/checkname/'+value+'/')
            .then(resp=>{
                console.log(resp);
                return callback()
            })
            .catch(err=>{
                console.log(err);
            })
            
        }else{
            return callback(new Error('用户名格式不正确'));
        }
      };
      var validatePass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入密码'));
        } 
        const passRules=/^[a-zA-Z][a-zA-Z0-9]{4,15}$/
        if(passRules.test(value)){
            return callback()
        }else{
            return callback(new Error('密码格式不正确'));
        }
      };
      var validatePass2 = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请再次输入密码'));
        } else if (value !== this.ruleForm.password) {
          callback(new Error('两次输入密码不一致!'));
        } else {
          callback();
        }
      };
      var checkMobile = (rule, value, callback) => {
        if (!value) {
          return callback(new Error('手机号不能为空'));
        }
        const userRules=/^1[3-9][0-9]{9}$/
        if(userRules.test(value)){
            axios.get('http://127.0.0.1:8000/user/checkmobile/'+value+'/')
            .then(resp=>{
                console.log(resp);
                return callback()
            })
            .catch(err=>{
                console.log(err);
            })
            
        }else{
            return callback(new Error('手机号格式不正确'));
        }
      };

      var checkImage = (rule, value, callback) => {
        if (!value) {
          return callback(new Error('验证码不能为空'));
        }
        const userRules=/^[a-zA-Z0-9]{4}$/
        if(userRules.test(value)){
            axios.get('http://127.0.0.1:8000/user/checkimage/'+this.imageId+'/')
            .then(resp=>{
                // console.log(resp);
                return callback()
            })
            .catch(err=>{
                console.log(err);
            })
            
        }else{
            return callback(new Error('验证码格式不正确'));
        }
      };

      return {
        imageId:'',
        imageUrl:'',
        ruleForm: {
          username:'',
          password: '',
          password2: '',
          mobile: '',
          imagecode:'',
        },
        rules: {
            username: [
            { validator: validaUser, trigger: 'blur' }
          ],
          password: [
            { validator: validatePass, trigger: 'blur' }
          ],
          password2: [
            { validator: validatePass2, trigger: 'blur' }
          ],
          mobile: [
            { validator: checkMobile, trigger: 'blur' }
          ],
          imagecode: [
            { validator: checkImage, trigger: 'blur' }
          ]
        }
      };
    },
    mounted(){
        this.getImage()
    },
    methods: {
        getImage(){
            this.imageId=uuid()
            this.imageUrl='http://127.0.0.1:8000/user/checkimage/'+this.imageId+'/'
        },
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            axios.post('http://127.0.0.1:8000/user/reg/',this.ruleForm)
            .then(resp=>{
                console.log(resp);
                alert('注册成功')
                this.$router.push('/login')
            })
            .catch(err=>{
                console.log(err);
            })
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    }
  }
</script>

<style>

</style>